Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports Csla
Imports Csla.Data
Imports Csla.Validation

<Serializable()> _
Public Class rw_leagueRoot
    Inherits Csla.BusinessBase(Of rw_leagueRoot)

#Region " Business Properties and Methods "

    ' declare members
    Private _leagueId As Integer = 0
    Private _leagueName As String = String.Empty
    Private _createDate As SmartDate = New SmartDate(DateTime.Today)

    ''' <summary>
    ''' Get the LeagueId of the current object.
    ''' </summary>
    ''' <value>A Integer that represents the LeagueId of the current object.</value>
    ''' <returns>Integer representing the return value.</returns>
    ''' <remarks></remarks>
    <System.ComponentModel.DataObjectField(True, False)> _
    Public ReadOnly Property LeagueId() As Integer
        Get
            CanReadProperty("LeagueId", True)
            Return _leagueId
        End Get
    End Property

    ''' <summary>
    ''' Get or Set the LeagueName of the current object.
    ''' </summary>
    ''' <value>A String that represents the LeagueName of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property LeagueName() As String
        Get
            CanReadProperty("LeagueName", True)
            Return _leagueName
        End Get
        Set(ByVal value As String)
            CanWriteProperty("LeagueName", True)
            If value Is Nothing Then value = String.Empty
            If Not _leagueName.Equals(value) Then
                _leagueName = value
                PropertyHasChanged("LeagueName")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CreateDate of the current object.
    ''' </summary>
    ''' <value>A DateTime that represents the CreateDate of the current object.</value>
    ''' <returns>DateTime representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CreateDate() As DateTime
        Get
            CanReadProperty("CreateDate", True)
            Return _createDate.Date
        End Get
        Set(ByVal value As DateTime)
            CanWriteProperty("CreateDate", True)
            If Not _createDate.Equals(value) Then
                _createDate.Date = value
                PropertyHasChanged("CreateDate")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get or Set the CreateDateString of the current object.
    ''' </summary>
    ''' <value>A String that represents the CreateDateString of the current object.</value>
    ''' <returns>String representing the return value.</returns>
    ''' <remarks></remarks>
    Public Property CreateDateString() As String
        Get
            CanReadProperty("CreateDateString", True)
            Return _createDate.Text
        End Get
        Set(ByVal value As String)
            CanWriteProperty("CreateDateString", True)
            If value Is Nothing Then value = String.Empty
            If Not _createDate.Equals(value) Then
                _createDate.Text = value
                PropertyHasChanged("CreateDateString")
            End If
        End Set
    End Property

    ''' <summary>
    ''' Get the unique id for the current object.
    ''' </summary>
    ''' <returns>A value that is the unique id of the object.</returns>
    ''' <remarks></remarks>
    Protected Overrides Function GetIdValue() As Object
        Return _leagueId
    End Function

#End Region ' Business Properties and Methods

#Region " Validation Rules "

    ''' <summary>
    ''' Method that allows the addition of custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddCustomRules()
        'todo: add custom/non-generated rules here...
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared custom business rules.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub AddSharedCustomRules()
        'todo: add custom/non-generated shared rules here...
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddInstanceBusinessRules()
        AddCustomRules()
        MyBase.AddBusinessRules()
    End Sub

    ''' <summary>
    ''' Method that allows the addition of shared common business rules.
    ''' </summary>
    ''' <remarks>These rules represent the database defaults that can be generated.</remarks>
    Private Sub AddSharedCommonRules()
        ' LeagueName rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringMaxLength, New Csla.Validation.CommonRules.MaxLengthRuleArgs("LeagueName", 50))
        ' CreateDate rules
        ValidationRules.AddRule(AddressOf Csla.Validation.CommonRules.StringRequired, "CreateDateString")
    End Sub

    ''' <summary>
    ''' Method used to instigate the addition of all Business rules both Custom and Common.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub AddBusinessRules()
        AddSharedCommonRules()
        AddSharedCustomRules()
        MyBase.AddBusinessRules()
    End Sub

#End Region ' Validation Rules

#Region " Authorization Rules "

    Protected Overrides Sub AddAuthorizationRules()
        'TODO: Define authorization rules in leagueRoot
        'AuthorizationRules.AllowRead("LeagueId", "leagueRootReadGroup")
        'AuthorizationRules.AllowRead("LeagueName", "leagueRootReadGroup")
        'AuthorizationRules.AllowRead("CreateDate", "leagueRootReadGroup")
        'AuthorizationRules.AllowRead("CreateDateString", "leagueRootReadGroup")

        'AuthorizationRules.AllowWrite("LeagueName", "leagueRootWriteGroup")
        'AuthorizationRules.AllowWrite("CreateDateString", "leagueRootWriteGroup")
    End Sub

    Public Shared Function CanGetObject() As Boolean
        'TODO: Define CanGetObject permission in leagueRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("leagueRootViewGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanAddObject() As Boolean
        'TODO: Define CanAddObject permission in leagueRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("leagueRootAddGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanEditObject() As Boolean
        'TODO: Define CanEditObject permission in leagueRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("leagueRootEditGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

    Public Shared Function CanDeleteObject() As Boolean
        'TODO: Define CanDeleteObject permission in leagueRoot
        Return True
        'If Csla.ApplicationContext.User.IsInRole("leagueRootDeleteGroup") Then
        '	Return True
        'End If
        'Return False
    End Function

#End Region ' Authorization Rules

#Region " Factory Methods "

    Private Sub New()
        ' require use of factory method 
    End Sub

    Public Shared Function NewleagueRoot(ByVal leagueId As Integer) As rw_leagueRoot
        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a leagueRoot")
        End If
        Return DataPortal.Create(Of rw_leagueRoot)(New Criteria(leagueId))
    End Function

    Public Shared Function GetleagueRoot(ByVal leagueId As Integer) As rw_leagueRoot
        If Not CanGetObject() Then
            Throw New System.Security.SecurityException("User not authorized to view a leagueRoot")
        End If
        Return DataPortal.Fetch(Of rw_leagueRoot)(New Criteria(leagueId))
    End Function

    Public Shared Sub DeleteleagueRoot(ByVal leagueId As Integer)
        If Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a leagueRoot")
        End If
        DataPortal.Delete(New Criteria(leagueId))
    End Sub

    Public Overrides Function Save() As rw_leagueRoot
        If IsDeleted AndAlso Not CanDeleteObject() Then
            Throw New System.Security.SecurityException("User not authorized to remove a leagueRoot")
        ElseIf IsNew AndAlso Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a leagueRoot")
        ElseIf Not CanEditObject() Then
            Throw New System.Security.SecurityException("User not authorized to update a leagueRoot")
        End If
        Return MyBase.Save()
    End Function

#End Region ' Factory Methods

#Region " Data Access "

#Region " Criteria "

    <Serializable()> _
    Private Class Criteria

        Public LeagueId As Integer

        Public Sub New(ByVal leagueId As Integer)
            Me.LeagueId = leagueId
        End Sub
    End Class

#End Region 'Criteria

#Region " Data Access - Create "

    <RunLocal()> _
    Private Overloads Sub DataPortal_Create(ByVal criteria As Criteria)
        _leagueId = criteria.LeagueId
        ValidationRules.CheckRules()
    End Sub

#End Region ' Data Access - Create

#Region " Data Access - Fetch "

    Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteFetch(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteFetch(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_getLeagueInfo"
            If criteria.LeagueId <> 0 Then
                cm.Parameters.AddWithValue("@iFilter", 1)
                cm.Parameters.AddWithValue("@iLeagueId", criteria.LeagueId)
            Else
                cm.Parameters.AddWithValue("@iFilter", 0)
                cm.Parameters.AddWithValue("@iLeagueId", 0)
            End If

            Using dr As SafeDataReader = New SafeDataReader(cm.ExecuteReader())
                FetchObject(dr)
                ValidationRules.CheckRules()

                'load child object(s)
                FetchChildren(dr)
            End Using
        End Using
    End Sub

    Private Sub FetchObject(ByVal dr As SafeDataReader)
        dr.Read()
        _leagueId = dr.GetInt32("leagueId")
        _leagueName = dr.GetString("leagueName")
        _createDate = dr.GetSmartDate("createDate", _createDate.EmptyIsMin)
    End Sub

    Private Sub FetchChildren(ByVal dr As SafeDataReader)
    End Sub

#End Region ' Data Access - Fetch

#Region " Data Access - Insert "

    Protected Overrides Sub DataPortal_Insert()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteInsert(cn)

            'update child object(s)
            UpdateChildren(cn)
        End Using
    End Sub

    Private Sub ExecuteInsert(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_insLeagueInfo"
            AddInsertParameters(cm)

            cm.ExecuteNonQuery()

        End Using
    End Sub

    Private Sub AddInsertParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@leagueName", _leagueName)
    End Sub

#End Region 'Data Access - Insert

#Region " Data Access - Update "

    Protected Overrides Sub DataPortal_Update()
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            If MyBase.IsDirty Then
                ExecuteUpdate(cn)
            End If

            ' update child object(s)
            UpdateChildren(cn)
        End Using

    End Sub

    Private Sub ExecuteUpdate(ByVal cn As SqlConnection)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "sp_updLeagueInfo"

            AddUpdateParameters(cm)

            cm.ExecuteNonQuery()

        End Using
    End Sub

    Private Sub AddUpdateParameters(ByVal cm As SqlCommand)
        cm.Parameters.AddWithValue("@leagueId", _leagueId)
        cm.Parameters.AddWithValue("@leagueName", _leagueName)
    End Sub

    Private Sub UpdateChildren(ByVal cn As SqlConnection)
        Dim divChild As rw_divisionChild
        divChild = rw_divisionChild.NewDivisionChild()
        divChild.Insert(cn, Me)
    End Sub

#End Region 'Data Access - Update

#Region " Data Access - Delete "

    Protected Overrides Sub DataPortal_DeleteSelf()
        DataPortal_Delete(New Criteria(_leagueId))
    End Sub

    Private Overloads Sub DataPortal_Delete(ByVal criteria As Criteria)
        Dim myConnectionObj As New connection
        Dim myConnection As SqlClient.SqlConnection
        myConnection = myConnectionObj.ReadMySettings()

        Using cn As SqlConnection = New SqlConnection(myConnection.ConnectionString.ToString)
            cn.Open()
            ExecuteDelete(cn, criteria)
        End Using
    End Sub

    Private Sub ExecuteDelete(ByVal cn As SqlConnection, ByVal criteria As Criteria)
        Using cm As SqlCommand = cn.CreateCommand()
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "esp_leagueroot_delete"

            cm.Parameters.AddWithValue("@leagueId", criteria.LeagueId)

            cm.ExecuteNonQuery()
        End Using
    End Sub

#End Region ' Data Access - Delete

#End Region 'Data Access


End Class

